<?php
class send_badge extends CI_Controller {
	function dox($id) {
		$this -> index($id);
	}

	function index($id) {
		$this -> pub -> logp('浏览徽章发放', '1-2');
		$da = $this -> pub -> get_setting('徽章发放', 'Badge');
		$q = $this -> db -> get_where('co_badge', array('id' => $id));
		$da['badge'] = $q -> row_array(0);
		$this -> load -> view('adminx/send_badge', $da);
	}

	function get() {
		$this -> pub -> logp('获取徽章发放数据', '1-2');
		$this -> db -> select('count(*)');
		$this -> db -> where('hid', $this -> input -> post('hid'));
		$this -> db -> where('state', 1);
		$q = $this -> db -> get_where('co_badges');

		$ls = $q -> row_array(0);
		$num = $ls['count(*)'];

		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> order_by('id', 'desc');
		$this -> db -> where('state', 1);
		$this -> db -> where('hid', $this -> input -> post('hid'));
		$q = $this -> db -> get_where('co_badges');
		$da['content'] = '';
		foreach ($q->result_array() as $v) {
			$qq = $this -> db -> get_where('member', array('mid' => $v['mid']));
			$m = $qq -> row_array(0);

			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $v['id'] . '</td>';
			$da['content'] .= '<td><img width="48" src="' . $m['headimgurl'] . '" /></td>';
			$da['content'] .= '<td>' . $m['nickname'] . '</td>';
			$da['content'] .= '<td>' . $m['name'] . '</td>';
			$da['content'] .= '<td>' . ($v['mktime'] ? date('Y-m-d', $v['mktime']) : '') . '</td>';
			$da['content'] .= '<td><button onclick="del(\'' . $v['id'] . '\',\'' . $m['nickname'] . '\')" type="button" class="btn red"><i class="fa fa-trash-o"></i> 删除</button>';
			$da['content'] .= '</td></tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

	function add() {
		$this -> pub -> logp('添加徽章发放数据', '1-2');
		$da['hid'] = $this -> input -> post('hid');
		$da['mid'] = $this -> input -> post('mid');
		$da['state'] = 1;
		$da['mktime'] = mktime();
		$this -> db -> insert('co_badges', $da);
		echo json_encode(array('type' => 1));
	}

	function del() {
		$this -> pub -> logp('删除徽章发放 - ' . $this -> input -> post('idx'), '1-2');
		$da['type'] = 1;
		$this -> db -> update('co_badges', array('state' => 2), array('id' => $this -> input -> post('idx')));
		echo json_encode($da);
	}

}
?>